package com.lkwlw.tplms.transport.action;

import java.util.List;
import java.util.Map;

import com.lkwlw.tplms.mapper.TransShipmentStationMapper;
import com.lkwlw.tplms.model.TransShipmentStation;
import com.lkwlw.tplms.model.TransShipmentStationExample;
import com.lkwlw.tplms.model.TransShipmentStationExample.Criteria;
import com.lkwlw.tplms.order.dao.mapper.OrderinfoMapper;
import com.lkwlw.tplms.order.dao.model.Orderinfo;
import com.lkwlw.tplms.system.dao.model.Admininfo;
import com.lkwlw.tplms.transport.dao.mapper.TransportInfoMapper;
import com.lkwlw.tplms.transport.dao.model.TransportInfo;
import com.lkwlw.tplms.transport.dao.model.TransportInfoExample;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class TransShipmentAction extends ActionSupport{
	private static final long serialVersionUID = -7206984461965021420L;
	
	private String orderId;
	private TransportInfoMapper transportInfoMapper;
	private TransShipmentStationMapper transShipmentMapper;
	private OrderinfoMapper orderInfoMapper;
	private Orderinfo order;
	private TransportInfo transportInfo;
	private TransShipmentStation transShipmentStation;	
	private String tipMessage;
	
	@Override
	public String execute() throws Exception {
		ActionContext context = ActionContext.getContext();
		Map session = context.getSession();
		Admininfo user = (Admininfo) session.get("admin");
		TransShipmentStationExample example = new TransShipmentStationExample();
		Criteria criteria = example.createCriteria();
		criteria.andStationAdminIdEqualTo(user.getId());
		List<TransShipmentStation> stationList = transShipmentMapper.selectByExample(null, example);
		if (null != stationList && 1 == stationList.size()) {
			transShipmentStation = stationList.get(0);
			session.put("transShipmentStation", transShipmentStation);
		}
		//tipMessage = "";
		order = orderInfoMapper.selectByPrimaryKey(orderId);
		transShipmentStation = transShipmentMapper.selectByPrimaryKey(null);
		//transportInfo = transportInfoMapper.selectByPrimaryKey(null);
		return SUCCESS;
	}
	
	public String loadOrder() throws Exception{
		order = orderInfoMapper.selectByPrimaryKey(orderId);
		if (null != order) {
			ActionContext context = ActionContext.getContext();
			Map session = context.getSession();
			transShipmentStation = (TransShipmentStation) session.get("transShipmentStation");
			TransportInfoExample example = new TransportInfoExample();
			com.lkwlw.tplms.transport.dao.model.TransportInfoExample.Criteria criteria 
				= example.createCriteria();
			criteria.andOrderidEqualTo(orderId);
			criteria.andUnloadEqualTo(1);
			criteria.andTransportnoEqualTo(transShipmentStation.getId());
			List<TransportInfo> tplist = transportInfoMapper.selectByExample(example);
			//判断是否重复装载
			if (null!=tplist && 0 < tplist.size()) {
				tipMessage = "duplicate_load";
				transportInfo = transportInfoMapper.selectByExample(example).get(0);
				execute();
			}else {
				transportInfo.setId(null);
				transportInfo.setOrderid(orderId);
				transportInfo.setTransportno(transShipmentStation.getId());
				transportInfo.setUnload(1);
				String stationType = transShipmentStation.getStationType();
				String stationName = transShipmentStation.getStationName();
				
				String remark = stationType.equals("海运") ? ("订单已上船，当前港口：" + stationName)
						: (stationType.equals("空运") ? ("订单已登机，当前机场：" + stationName)
						: stationType.equals("铁路") ? ("订单已上车，当前站点：" + stationName) : "");
				transportInfo.setRemark(remark);
				
				// 更新订单状态				
				if(!"运输中".equals(order.getState())){
					orderInfoMapper.updateStateByPrimaryKey(order);
				}				
				transportInfoMapper.insertSelective(transportInfo);
				
				transportInfo = transportInfoMapper.selectByExample(example).get(0);
				execute();
				tipMessage = "success";
			}
		}else {
			tipMessage = "norecord";
		}	
		orderId = "";
		return SUCCESS;
	}
	
	public String unLoadOrder() throws Exception{
		order = orderInfoMapper.selectByPrimaryKey(orderId);
		if (null != order) {
			ActionContext context = ActionContext.getContext();
			Map session = context.getSession();
			transShipmentStation = (TransShipmentStation) session.get("transShipmentStation");
			TransportInfoExample example = new TransportInfoExample();
			com.lkwlw.tplms.transport.dao.model.TransportInfoExample.Criteria criteria 
				= example.createCriteria();
			criteria.andOrderidEqualTo(orderId);
			criteria.andUnloadEqualTo(2);
			criteria.andTransportnoEqualTo(transShipmentStation.getId());
			List<TransportInfo> tplist = transportInfoMapper.selectByExample(example);
			//判断是否重复装载
			if (null!=tplist && 0 < tplist.size()) {
				tipMessage = "duplicate_load";
				transportInfo = transportInfoMapper.selectByExample(example).get(0);
				execute();
			}else {
				transportInfo.setId(null);
				transportInfo.setOrderid(orderId);
				transportInfo.setTransportno(transShipmentStation.getId());
				transportInfo.setUnload(2);
				String stationType = transShipmentStation.getStationType();
				String stationName = transShipmentStation.getStationName();
				
				String remark = stationType.equals("海运") ? ("订单已下船，当前港口：" + stationName)
						: (stationType.equals("空运") ? ("订单已下机，当前机场：" + stationName)
						: stationType.equals("铁路") ? ("订单已下车，当前站点：" + stationName) : "");
				transportInfo.setRemark(remark);
				
				// 更新订单状态				
				if(!"运输中".equals(order.getState())){
					orderInfoMapper.updateStateByPrimaryKey(order);
				}				
				transportInfoMapper.insertSelective(transportInfo);
				
				transportInfo = transportInfoMapper.selectByExample(example).get(0);
				execute();
				tipMessage = "success";
			}
		}else {
			tipMessage = "norecord";
		}	
		orderId = "";
		return SUCCESS;
	}
	
	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public Orderinfo getOrder() {
		return order;
	}

	public void setOrder(Orderinfo order) {
		this.order = order;
	}

	public TransportInfo getTransportInfo() {
		return transportInfo;
	}

	public void setTransportInfo(TransportInfo transportInfo) {
		this.transportInfo = transportInfo;
	}

	public void setTransportInfoMapper(TransportInfoMapper transportInfoMapper) {
		this.transportInfoMapper = transportInfoMapper;
	}

	public void setTransShipmentMapper(
			TransShipmentStationMapper transShipmentMapper) {
		this.transShipmentMapper = transShipmentMapper;
	}

	public void setOrderInfoMapper(OrderinfoMapper orderInfoMapper) {
		this.orderInfoMapper = orderInfoMapper;
	}
	
	public TransShipmentStation getTransShipmentStation() {
		return transShipmentStation;
	}

	public void setTransShipmentStation(TransShipmentStation transShipmentStation) {
		this.transShipmentStation = transShipmentStation;
	}

	public String getTipMessage() {
		return tipMessage;
	}

	public void setTipMessage(String tipMessage) {
		this.tipMessage = tipMessage;
	}

}
